package com.xp.tinyurl.admin.controller;

import com.xp.tinyurl.admin.auth.UserThreadLocal;
import com.xp.tinyurl.admin.dto.AppRegisterDTO;
import com.xp.tinyurl.admin.dto.AppRegisterQueryDTO;
import com.xp.tinyurl.admin.dto.AppUpdateDTO;
import com.xp.tinyurl.common.base.PageList;
import com.xp.tinyurl.common.base.ResultVO;
import com.xp.tinyurl.common.base.ResultVOBuilder;
import com.xp.tinyurl.common.enums.BusinessLineEnum;
import com.xp.tinyurl.common.utils.AppKeyGenUtils;
import com.xp.tinyurl.model.Register;
import com.xp.tinyurl.service.AppRegisterService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description 应用信息
 * @Author chengguo.pei
 * @Date 2021/9/22 17:30
 */
@Slf4j
@RequestMapping("/admin/app")
@RestController
public class AppInfoController {

    @Autowired
    private AppRegisterService appRegisterService;

    @Autowired
    private UserThreadLocal userThreadLocal;

    /**
     * 查询短链接APP注册列表
     * @param queryDTO
     * @return
     */
    @RequestMapping(value = "/getAppRegisterList")
    public ResultVO getAppRegisterList(AppRegisterQueryDTO queryDTO) {
        int startPage = queryDTO.getPageNo();

        Map<String, Object> param = new HashMap<>(4);
        param.put("pageSize", queryDTO.getPageSize());
        param.put("startPage", startPage);
        param.put("businessLine", queryDTO.getBusinessLine());
        param.put("module", queryDTO.getModule());

        List<Register> list = appRegisterService.getPageList(param);
        if(!CollectionUtils.isEmpty(list)){
            list.forEach(register -> {
                register.setBusinessLineName(BusinessLineEnum.getByCode(register.getBusinessLine()).getDesc());
            });
        }
        Integer count = appRegisterService.getPageListCount(param);
        PageList<Register> pageList = PageList.build(list, count);
        return ResultVOBuilder.success(pageList);

    }

    /**
     * 注册应用短链接
     * @param appRegisterDTO
     * @return
     */
    @RequestMapping(value = "/appRegister")
    public ResultVO appRegister(AppRegisterDTO appRegisterDTO) {
        Register registerDB = appRegisterService.getAppRegisterByModule(appRegisterDTO.getModule());
        if(registerDB != null){
            return ResultVOBuilder.failure("当前模块已存在，请更换其他模块名称！");
        }
        Register register = new Register();
        register.setBusinessLine(appRegisterDTO.getBusinessLine());
        register.setAppKey(AppKeyGenUtils.genKey(appRegisterDTO.getModule()));
        register.setModule(appRegisterDTO.getModule());
        register.setCreator(userThreadLocal.get());
        register.setModifier(userThreadLocal.get());
        register.setLocked(false);
        register.setHystrix(false);
        register.setThreshold(0);
        appRegisterService.save(register);
        return ResultVOBuilder.success();
    }

    /**
     * 更新应用
     * @param updateDTO
     * @return
     */
    @RequestMapping(value = "/updateAppInfo")
    public ResultVO updateAppInfo(AppUpdateDTO updateDTO) {
        Register appInfo = appRegisterService.getAppRegisterByAppKey(updateDTO.getAppKey());
        if (appInfo == null) {
            return ResultVOBuilder.failure("appKey不存在");
        }
        if (updateDTO.getModule() != null) {
            appInfo.setModule(updateDTO.getModule());
        }
        if (updateDTO.getLocked() != null) {
            appInfo.setLocked(updateDTO.getLocked());
        }
        if (updateDTO.getHystrix() != null) {
            appInfo.setHystrix(updateDTO.getHystrix());
        }
        if (updateDTO.getThreshold() != null) {
            appInfo.setThreshold(updateDTO.getThreshold());
        }
        appRegisterService.updateAppInfoAndCache(appInfo);
        return ResultVOBuilder.success();
    }

    /**
     * 删除短链接应用
     * @param appKey
     * @param deleted true：删除，false：取消删除
     * @return
     */
    @RequestMapping(value = "/deleteApp")
    public ResultVO deleteApp(String appKey,Boolean deleted) {

        Register appInfo = appRegisterService.getAppRegisterByAppKey(appKey);
        if (appInfo == null) {
            return ResultVOBuilder.failure("appKey不存在");
        }
        appInfo.setDeleted(deleted);
        appRegisterService.updateAppInfoAndCache(appInfo);
        return ResultVOBuilder.success();
    }

}
